// components/Cashier/Cashier.js
Component({
  properties: {
    itemObj: {
      type: Object,
      value: {
        address: '店铺地址信息店铺地址信息店铺地址信息',
        distant: 900,
        distantUnit: '米',
        name: '张三',
        tel: '1888888888',
        data: "",
        time: ''
      }
    },
    needTime: {
      type: Boolean,
      value: false
    },
    orderInfo: {
      type: Object,
    },
    orderType: {
      type: Number,
      //订单类型， 0-预订 1-预约 2-积分兑换
    },
    totalPrice: {
      type: String
    },
  },

  data: {
    noticeString: '温馨提示：若未在预约时间到店进行消费，该订单将自动取消',
    columns: [{
        values: [],
      },
      {
        values: [
          '8:00~9:00',
          '9:00~10:00',
          '10:00~11:00',
          '11:00~12:00',
          '12:00~13:00',
          '13:00~14:00',
          '14:00~15:00',
          '15:00~16:00',
          '16:00~17:00',
          '17:00~18:00',
          '18:00~19:00',
          '19:00~20:00',
          '20:00~21:00',
          '21:00~22:00',
          '22:00~23:00',
          '23:00~24:00',
        ],
      }
    ],
    discountColumns: [{
      values: ['10.00', '20.00', '30.00'],
      defaultIndex: 1
    }],
    picker_show: false,
    discount_show: false,
    //用于判断是否选择过时间，默认下单时显示提示文字
    dateChoosed: false,
  },
  created() {

  },
  ready() {
    if (this.data.orderType === 1) {
      this.getDate();
    }
    // console.log(this.data.orderType)
    this.getTotalPrice();
  },
  methods: {
    getDate() {
      // 获取当前日期的之后六天日期  日期格式2021-05-01
      var today = new Date();
      var dateArr = []
      for (var i = 0; i < 7; i++) {
        var newDate = new Date(today.getTime() + i * 1000 * 60 * 60 * 24)
        var year = newDate.getFullYear()
        var month = (parseInt(newDate.getMonth()) + 1) > 9 ? (parseInt(newDate.getMonth()) + 1) : "0" + (parseInt(newDate.getMonth()) + 1)
        var day = (newDate.getDate()) > 9 ? newDate.getDate() : "0" + newDate.getDate()
        var fullDate = `${year}-${month}-${day}`
        dateArr.push(fullDate)
      }
      this.setData({
        'columns[0].values': dateArr
      })
      // console.log(this.data.columns[0])
    },
    showDiscount() {
      this.setData({
        discount_show: true
      })
    },
    showPicker() {
      this.setData({
        picker_show: true
      });
    },

    ClosePicker() {
      this.setData({
        picker_show: false,
        discount_show: false
      });
    },
    getTotalPrice() {
      let res = 0
      for (let item of this.data.orderInfo.goodArr) {
        res += parseInt(item.price) * parseInt(item.count);
      }
      //只有在 orderType 等于 0 的时候才需要减去优惠折扣
      this.setData({
        totalPrice: this.data.orderType === 0 ? (res - parseInt(this.data.orderInfo.discount)) : res
      })
      // console.log(this.data.totalPrice)
    },

    cancelPicker() {
      this.ClosePicker()
    },

    confirmPicker(e) {
      this.ClosePicker()
      this.setData({
        dateChoosed: true,
        'itemObj.data': e.detail.value[0],
        'itemObj.time': e.detail.value[1],
      })
      console.log(this.data.itemObj)
    },

    confirmDiscount(e) {
      this.ClosePicker();
      // console.log(e.detail)
      this.setData({
        'orderInfo.discount': e.detail.value[0],
      })
      //重新计算总价
      this.getTotalPrice()
    },
    onPay() {
      //判断信息是否合理
      //向服务器提交数据 申请结算   订单生成成功，此页面应被销毁， 同时展示支付成功界面
      console.log('结算')
      //关闭当前页面,或者直接隐藏结算表单，显示结算成功，三面后自动跳转，跳转到该订单页面
      wx.navigateTo({
        url: '/pages/orderDetail/orderDetail',
      })
    },
  },


})